package com.bob.shirodemo.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class DemoController {

    @ResponseBody
    @RequestMapping("test")
    public String test() {
        return "hello world";
    }

    @ResponseBody
    @RequestMapping("login")
    public String login() {
        Subject subject = SecurityUtils.getSubject();
        //此类型的token.getPrincipal()获得值就是用户名
        UsernamePasswordToken token = new UsernamePasswordToken("管理员", "123");
        try {
            subject.login(token);
//            subject.getSession().getId();
//                BaseResponse<String> baseResponse = new BaseResponse<>();
        }catch ( UnknownAccountException uae ) {
            log.error("==========>>>logging occurred a error：", uae);
            //username wasn't in the system, show them an error message?
        } catch ( IncorrectCredentialsException ice ) {
            //password didn't match, try again?
            log.error("==========>>>logging occurred a error：", ice);
        } catch ( LockedAccountException lae ) {
            //account for that username is locked - can't login.  Show them a message?
            log.error("==========>>>logging occurred a error：", lae);
        }
        return "login success:" +  subject.getSession().getId();
    }
}
